package com.hxt.util;

import java.io.InputStream;
import java.util.Map;

import org.apache.commons.lang3.BooleanUtils;

import com.google.common.collect.ImmutableMap;

import io.minio.MinioClient;

public class MinioClientUtils {

	public static String minio_url = CONSTANT.MINIO.URL;

	public static String minio_access_key = CONSTANT.MINIO.ACCESS_KEY;

	public static String minio_secret_key = CONSTANT.MINIO.SECRET_KEY;

	public static String minio_bucketName = CONSTANT.MINIO.BUCKET_NAME;

	/**
	 * @Title: upload
	 * @Description:上传主功能
	 * @return 返回URL
	 * @throws Exception
	 */
	public static Map<String, String> upload(InputStream inputStream, String suffix, String contentType)
			throws Exception {

		MinioClient minioClient = new MinioClient(minio_url, minio_access_key, minio_secret_key);
		boolean isExist = minioClient.bucketExists(minio_bucketName);
		if (BooleanUtils.negate(isExist)) {
			minioClient.makeBucket(minio_bucketName);
		}

		String objectName = StringUtil.getUUID() + (suffix != null ? suffix : "");
		minioClient.putObject(minio_bucketName, objectName, inputStream, contentType);
		String url = minioClient.presignedGetObject(minio_bucketName, objectName);
		return ImmutableMap.of("url", url, "key", objectName);

	}

	public static InputStream getObjectToInputStream(String key) throws Exception {
		MinioClient minioClient = new MinioClient(minio_url, minio_access_key, minio_secret_key);
		minioClient.statObject(minio_bucketName, key);

		return minioClient.getObject(minio_bucketName, key);
	}

	public static String presignedURL(String key) {

		try {
			MinioClient minioClient = new MinioClient(minio_url, minio_access_key, minio_secret_key);
			return minioClient.presignedGetObject(minio_bucketName, key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";

	}

	public static void main(String[] args) throws Exception {

	}
}
